package com.cizzy.video.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.cizzy.video.common.Result;
import com.cizzy.video.entity.dao.UserDAO;
import com.cizzy.video.entity.dao.UserCountDAO;
import com.cizzy.video.entity.dao.VideoDAO;
import com.cizzy.video.entity.dao.VideoCountDAO;
import com.cizzy.video.entity.dto.UserDTO;
import com.cizzy.video.entity.dto.VideoDTO;
import com.cizzy.video.entity.vo.PageRequest;
import com.cizzy.video.entity.vo.VideoVo;
import com.cizzy.video.feign.UserFeign;
import com.cizzy.video.service.IVideoCountService;
import com.cizzy.video.service.IVideoService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.annotation.Resource;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.stream.IntStream;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.web.multipart.MultipartFile;

//import org.redisson.api.RedissonClient;

//import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import static com.cizzy.video.config.CizzyFinal.PAGE_SIZE;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author crissy-月下闲人
 * @since 2023-10-29
 */
@Controller
@CrossOrigin
@RequestMapping("/video")
@SentinelResource("video")
public class VideoController {

    @Resource
    private IVideoService videoService;

    @Resource
    private LoadBalancerClient loadBalancerClient;


    @Autowired
    private RestTemplate template;

    /**
     * 获取推荐视频
     *
     * @param userId 用户ID
     * @return 返回包含推荐视频的结果对象
     */
    @ResponseBody
    @GetMapping("/getRecommendVideo/{userId}")
    public Result<VideoDTO> getRecommendVideo(@PathVariable("userId") Long userId) {
        return videoService.getRecommendVideo(userId);
    }

    /**
     * 推送视频
     *
     * @param pageRequest 页面请求对象
     * @return 返回包含提交的视频列表的结果对象
     */
    @ResponseBody
    @PostMapping("/feed")
    public Result<List<VideoDTO>> feed(@RequestBody PageRequest pageRequest) {
        return videoService.feed(pageRequest);
    }


    /**
     * 发布视频
     *
     * @return
     */

       /**
     * 发布视频
     *
     * @param videoVo 视频信息
     * @param userId 用户ID
     * @return 结果对象包含发布成功的视频信息
     * @throws IOException 输入参数异常时抛出IO异常
     */
    @ResponseBody
    @PostMapping("/publishVideo")
    @ConfigurationProperties(prefix = "qiniu")
    public Result<VideoDAO> publishVideo(VideoVo videoVo, @RequestHeader("userId") String userId) throws IOException {
        return videoService.publishVideo(videoVo, userId);
    }

    /**
     * 获取视频列表
     *
     * @return 结果对象包含视频列表
     */
    @ResponseBody
    @GetMapping("/getVideoList")
    public Result<List<VideoDAO>> getVideoList() {
        return videoService.getVideoList(10);
    }

    /**
     * 根据用户ID获取视频列表
     *
     * @param id 用户ID
     * @return 结果对象包含视频列表
     */
    @ResponseBody
    @GetMapping("/getVideoListByUserId/{id}")
    public Result<List<VideoDTO>> getVideoListByUserId(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        return videoService.getVideoListByUserId(id);
    }


    /**
     * 根据视频ID列表获取视频列表
     *
     * @param ids 视频ID列表
     * @return 结果与视频列表
     */
    @ResponseBody
    @PostMapping("/getVideoListByIds")
    public Result<List<VideoDTO>> getVideoListByIds(@RequestBody List<Long> ids) {
        return videoService.getVideoListByIds(ids);
    }

    /**
     * 根据视频ID获取视频信息
     *
     * @param id  视频ID
     * @return 结果与视频信息
     */
    @ResponseBody
    @GetMapping("/getVideoById/{id}")
    public Result<VideoDAO> getVideoById(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        return videoService.getVideoById(id);
    }

    /**
     * 根据用户ID获取带用户信息的视频信息
     *
     * @param id 用户ID
     * @return 结果与视频信息
     */
    @ResponseBody
    @GetMapping("/getVideoByUserId/{id}")
    public Result<VideoDTO> getVideoByUserId(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        return videoService.getVideoDTOById(id);
    }


    /**
     * 根据类型获取视频列表
     * @param type 类型
     * @param num 数量
     * @return 结果和视频列表
     */
    @ResponseBody
    @GetMapping("/getVideoListByType")
    public Result<List<VideoDAO>> getVideoListByType(@RequestParam("type") String type, Integer num) {
        return videoService.getVideoListByType(type, num);
    }

    /**
     * 根据类型获取视频
     * @param type 类型
     * @return 结果和视频
     */
    @ResponseBody
    @GetMapping("/getVideoByType")
    public Result<VideoDAO> getVideoByType(@RequestParam("type") String type) {
        return videoService.getVideoByType(type);
    }

    /**
     * 搜索视频页面
     * @param keyword 关键词
     * @param videoNum 视频数量
     * @return 结果和视频列表
     */
    @ResponseBody
    @GetMapping("/searchVideoPage/{keyword}/{videoNum}")
    public Result<List<VideoDTO>> searchVideoPage(@PathVariable("keyword") String keyword, @PathVariable("videoNum") Long videoNum) {
        return videoService.searchVideoPage(keyword, videoNum);
    }

}
